package com.geoway.adf.report.excel.sheet;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geoway.adf.report.excel.conditionformat.ConditionFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lzw
 * @Date: 2024/1/17 16:23
 * @Description: 单元格公式信息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString
public class Calculate {
    /**
     * 单元格所在的行
     */
    private Integer r;
    /**
     * 单元格所在的列
     */
    private Integer c;
    /**
     * SheetID
     */
    private String index;
    /**
     * 公式信息，包含公式计算结果和公式字符串
     */
    private List<String> func;
    /**
     * "w"：采用深度优先算法 "b":普通计算
     */
    private String color;

    private JSONObject parent;

    @JsonProperty("chidren")
    private JSONObject children;

    private String times;

    public static void main(String[] args) throws JsonProcessingException {
        String source = "[{\"r\":6,\"c\":3,\"index\":1,\"func\":[true,23.75,\"=AVERAGE(D3:D6)\"],\"color\":\"w\",\"parent\":null,\"chidren\":{},\"times\":0},{\"r\":7,\"c\":3,\"index\":1,\"func\":[true,30,\"=MAX(D3:D6)\"],\"color\":\"w\",\"parent\":null,\"chidren\":{},\"times\":0}]";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(source);
        List<Calculate> calculates = new ArrayList<>();
        if(jsonNode.isArray()){
            for(int i=0; i<jsonNode.size(); i++){
                JsonNode node = jsonNode.get(i);
                calculates.add(mapper.treeToValue(node, Calculate.class));
            }
        }
        System.out.println(calculates);
    }
}
